package com.easyattendance.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyattendance.model.StudentAttendance;
import com.easyattendance.services.AttendanceService;

/**
 * Servlet implementation class EditAttendanceServlet
 */
@WebServlet("/EditAttendanceServlet")
public class EditAttendanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html");
    	
    	AttendanceService attendanceService = new AttendanceService();
    	
    	ArrayList<StudentAttendance> originalAttendanceList = (ArrayList<StudentAttendance>) request.getSession().getAttribute("attendanceList");
    	/*String[] studentRollNos = new String[originalAttendanceList.size()];
    	String[] newStatuses = new String[originalAttendanceList.size()];*/
    	
    	String[] studentRollNos = request.getParameterValues("student_roll_no");
    	String[] newStatuses = request.getParameterValues("student_status");
    	
    	String classId = request.getParameter("class_id");
    	Date date1=null;
    	java.sql.Date date = null;
		try {
			date1 = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("date"));
			date=new java.sql.Date(date1.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if(classId == null || request.getParameter("date") == null) {
			request.setAttribute("nullValues", "Value cannot be null. Select again.");
        	RequestDispatcher rd = request.getRequestDispatcher("views/teacher/editAttendance.jsp");
        	rd.forward(request, response);
		}
    	
    	/*for(int i=0; i<originalAttendanceList.size(); i++) {
    		studentRollNos[i] = request.getParameter("stud_roll_no"+i);
    		newStatuses[i] = request.getParameter("student_status"+i);
    	}*/
    	
    	boolean result = attendanceService.editAttendance(studentRollNos, date, classId, newStatuses);
    	
    	if (result) {
    		request.setAttribute("attendanceEditSuccess", "Attendance List edited successfully.");
        	RequestDispatcher rd = request.getRequestDispatcher("views/teacher/editAttendance.jsp");
        	rd.forward(request, response);
    	}
    	else {
    		request.setAttribute("attendanceEditNotSuccess", "Attendance List could not be edited successfully.");
        	RequestDispatcher rd = request.getRequestDispatcher("views/teacher/editAttendance.jsp");
        	rd.forward(request, response);
    	}
    }
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
